import java.util.ArrayList;
import java.util.Comparator;

/**
 * @author akaovi
 * @version 1.0
 * @description
 * @date 2022/11/14 21:01
 * @since 2022/11/14 21:01
 */
public class ClassRoom {

    private ArrayList<Student> cls = new ArrayList<>();

    public ClassRoom() {
    }

    public ClassRoom(ArrayList<Student> cls) {
        this.cls = cls;
    }

    public ArrayList<Student> getCls() {
        return cls;
    }

    public void add(Student student) {
        cls.add(student);
    }

    public void list() {
        cls.forEach(System.out::println);
    }

    public Student getOne(int index) {
        return cls.get(index);
    }

    public Student getOne(String name) {
        for (Student s : cls) {
            if (name.equals(s.getName())) {
                return s;
            }
        }
        return null;
    }

    public void sortStudent() {
        cls.sort(Comparator.comparing(Student::getName));
    }

    public static void main(String[] args) {
        ClassRoom classRoom = new ClassRoom();
        classRoom.add(new Student("Judy"));
        classRoom.add(new Student("Tom"));
        classRoom.add(new Student("Dim"));
        classRoom.add(new Student("Jerry"));
        classRoom.add(new Student("Tim"));

        System.out.println("展示所有学生");
        classRoom.list();
        System.out.println("获取索引为1的学生");
        System.out.println(classRoom.getOne(1));
        System.out.println("获取姓名为Dim的学生");
        System.out.println(classRoom.getOne("Dim"));
        System.out.println("排序后");
        classRoom.sortStudent();
        classRoom.list();
    }
}
